import cython
import numpy as np


def generate_cloud_mesh_jit(center_pos, radius, face_vertices, clouds_layout):
    min_x: cython.int = int(center_pos[0] - radius)
    min_y: cython.int = int(center_pos[1] - radius)
    max_x: cython.int = int(center_pos[0] + radius)
    max_y: cython.int = int(center_pos[1] + radius)

    max_vertices: cython.int = (radius * 2 - 1) * (radius * 2 - 1) * 6 * 4
    vertices = np.empty(max_vertices * 10, dtype=np.float32)
    indices = np.empty(max_vertices * 6, dtype=np.uint32)
    vertex_count: cython.int = 0
    index_count: cython.int = 0

    # vertices, indices = [], []
    index: cython.int = 0
    for x in range(min_x, max_x):
        for y in range(min_y, max_y):
            l:cython.int = clouds_layout[x & 0xff, y & 0xff]
            if l == 0:
                continue

            dx:cython.float = x - center_pos[0]
            dy:cython.float = y - center_pos[1]
            start: cython.int = 4 * 4
            vertices[vertex_count:vertex_count + 4 * 6] = [
                face_vertices[start][0] + dx,
                face_vertices[start][1],
                face_vertices[start][2] + dy,
                face_vertices[start][3],
                face_vertices[start][4],
                face_vertices[start][5],

                face_vertices[start + 1][0] + dx,
                face_vertices[start + 1][1],
                face_vertices[start + 1][2] + dy,
                face_vertices[start + 1][3],
                face_vertices[start + 1][4],
                face_vertices[start + 1][5],

                face_vertices[start + 2][0] + dx,
                face_vertices[start + 2][1],
                face_vertices[start + 2][2] + dy,
                face_vertices[start + 2][3],
                face_vertices[start + 2][4],
                face_vertices[start + 2][5],

                face_vertices[start + 3][0] + dx,
                face_vertices[start + 3][1],
                face_vertices[start + 3][2] + dy,
                face_vertices[start + 3][3],
                face_vertices[start + 3][4],
                face_vertices[start + 3][5]
            ]
            vertex_count += 4 * 6
            indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
            index_count += 6
            index += 4

            start: cython.int = 4 * 5
            vertices[vertex_count:vertex_count + 4 * 6] = [
                face_vertices[start][0] + dx,
                face_vertices[start][1],
                face_vertices[start][2] + dy,
                face_vertices[start][3],
                face_vertices[start][4],
                face_vertices[start][5],

                face_vertices[start + 1][0] + dx,
                face_vertices[start + 1][1],
                face_vertices[start + 1][2] + dy,
                face_vertices[start + 1][3],
                face_vertices[start + 1][4],
                face_vertices[start + 1][5],

                face_vertices[start + 2][0] + dx,
                face_vertices[start + 2][1],
                face_vertices[start + 2][2] + dy,
                face_vertices[start + 2][3],
                face_vertices[start + 2][4],
                face_vertices[start + 2][5],

                face_vertices[start + 3][0] + dx,
                face_vertices[start + 3][1],
                face_vertices[start + 3][2] + dy,
                face_vertices[start + 3][3],
                face_vertices[start + 3][4],
                face_vertices[start + 3][5]
            ]
            vertex_count += 4 * 6
            indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
            index_count += 6
            index += 4

            ll = clouds_layout[(x - 1) & 0xff, y & 0xff]
            if ll == 0:
                start: cython.int = 4 * 2
                vertices[vertex_count:vertex_count + 4 * 6] = [
                    face_vertices[start][0] + dx,
                    face_vertices[start][1],
                    face_vertices[start][2] + dy,
                    face_vertices[start][3],
                    face_vertices[start][4],
                    face_vertices[start][5],

                    face_vertices[start + 1][0] + dx,
                    face_vertices[start + 1][1],
                    face_vertices[start + 1][2] + dy,
                    face_vertices[start + 1][3],
                    face_vertices[start + 1][4],
                    face_vertices[start + 1][5],

                    face_vertices[start + 2][0] + dx,
                    face_vertices[start + 2][1],
                    face_vertices[start + 2][2] + dy,
                    face_vertices[start + 2][3],
                    face_vertices[start + 2][4],
                    face_vertices[start + 2][5],

                    face_vertices[start + 3][0] + dx,
                    face_vertices[start + 3][1],
                    face_vertices[start + 3][2] + dy,
                    face_vertices[start + 3][3],
                    face_vertices[start + 3][4],
                    face_vertices[start + 3][5]
                ]
                vertex_count += 4 * 6
                indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                index_count += 6
                index += 4
            lr = clouds_layout[(x + 1) & 0xff, y & 0xff]
            if lr == 0:
                start: cython.int = 4 * 3
                vertices[vertex_count:vertex_count + 4 * 6] = [
                    face_vertices[start][0] + dx,
                    face_vertices[start][1],
                    face_vertices[start][2] + dy,
                    face_vertices[start][3],
                    face_vertices[start][4],
                    face_vertices[start][5],

                    face_vertices[start + 1][0] + dx,
                    face_vertices[start + 1][1],
                    face_vertices[start + 1][2] + dy,
                    face_vertices[start + 1][3],
                    face_vertices[start + 1][4],
                    face_vertices[start + 1][5],

                    face_vertices[start + 2][0] + dx,
                    face_vertices[start + 2][1],
                    face_vertices[start + 2][2] + dy,
                    face_vertices[start + 2][3],
                    face_vertices[start + 2][4],
                    face_vertices[start + 2][5],

                    face_vertices[start + 3][0] + dx,
                    face_vertices[start + 3][1],
                    face_vertices[start + 3][2] + dy,
                    face_vertices[start + 3][3],
                    face_vertices[start + 3][4],
                    face_vertices[start + 3][5]
                ]
                vertex_count += 4 * 6
                indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                index_count += 6
                index += 4
            lu = clouds_layout[x & 0xff, (y + 1) & 0xff]
            if lu == 0:
                start: cython.int = 4 * 0
                vertices[vertex_count:vertex_count + 4 * 6] = [
                    face_vertices[start][0] + dx,
                    face_vertices[start][1],
                    face_vertices[start][2] + dy,
                    face_vertices[start][3],
                    face_vertices[start][4],
                    face_vertices[start][5],

                    face_vertices[start + 1][0] + dx,
                    face_vertices[start + 1][1],
                    face_vertices[start + 1][2] + dy,
                    face_vertices[start + 1][3],
                    face_vertices[start + 1][4],
                    face_vertices[start + 1][5],

                    face_vertices[start + 2][0] + dx,
                    face_vertices[start + 2][1],
                    face_vertices[start + 2][2] + dy,
                    face_vertices[start + 2][3],
                    face_vertices[start + 2][4],
                    face_vertices[start + 2][5],

                    face_vertices[start + 3][0] + dx,
                    face_vertices[start + 3][1],
                    face_vertices[start + 3][2] + dy,
                    face_vertices[start + 3][3],
                    face_vertices[start + 3][4],
                    face_vertices[start + 3][5]
                ]
                vertex_count += 4 * 6
                indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                index_count += 6
                index += 4
            ld = clouds_layout[x & 0xff, (y - 1) & 0xff]
            if ld == 0:
                start: cython.int = 4 * 1
                vertices[vertex_count:vertex_count + 4 * 6] = [
                    face_vertices[start][0] + dx,
                    face_vertices[start][1],
                    face_vertices[start][2] + dy,
                    face_vertices[start][3],
                    face_vertices[start][4],
                    face_vertices[start][5],

                    face_vertices[start + 1][0] + dx,
                    face_vertices[start + 1][1],
                    face_vertices[start + 1][2] + dy,
                    face_vertices[start + 1][3],
                    face_vertices[start + 1][4],
                    face_vertices[start + 1][5],

                    face_vertices[start + 2][0] + dx,
                    face_vertices[start + 2][1],
                    face_vertices[start + 2][2] + dy,
                    face_vertices[start + 2][3],
                    face_vertices[start + 2][4],
                    face_vertices[start + 2][5],

                    face_vertices[start + 3][0] + dx,
                    face_vertices[start + 3][1],
                    face_vertices[start + 3][2] + dy,
                    face_vertices[start + 3][3],
                    face_vertices[start + 3][4],
                    face_vertices[start + 3][5]
                ]
                vertex_count += 4 * 6
                indices[index_count:index_count + 6] = [index, index + 1, index + 2, index, index + 2, index + 3]
                index_count += 6
                index += 4
    return vertices[:vertex_count], indices[:index_count]
